<% title _("Recurring logics") %>
<% title_actions SETTINGS[:version].short <= '1.13' ? help_path : help_button %>

<% if @errors %>
<%= alert(:class => 'alert-info', :id => 'multiple-alert', :close => false, :header => '', :text => @errors) %>
<% end %>

<% if authorized_for(:permission => :edit_recurring_logics, :auth_object => @recurring_logics) %>
  <% title_actions link_to(_('Clear Cancelled'),
              clear_cancelled_foreman_tasks_recurring_logics_path,
              class:  ['btn', 'btn-sm', 'btn-danger'],                  
              :'data-toggle' => "modal",
              :'data-target' => "#clear_modal") 
              %>

  <div class="modal fade" id="clear_modal" tabindex="-1" role="dialog" aria-labelledby="Deploy" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h2 class="modal-title" id="deploy_modal_label">
            <span class="glyphicon glyphicon-warning-sign"></span>
            <%= _("Clear Cancelled") %>
          </h2>
        </div>
        <div class="modal-body">
          <%= _("This action will delete all cancelled recurring logics. Please note that this action can't be reversed.") %>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal"><%= _("Cancel") %></button>
          <%= link_to(_('Clear Cancelled'), clear_cancelled_foreman_tasks_recurring_logics_path, method: :post, class: 'btn btn-danger modal-submit') %>
        </div>
      </div>
    </div>
  </div>
<% end %>

<table class="<%= table_css_classes('table-condensed table-fixed') %>">
  <thead>
    <th class="col-md-1"><%= N_("ID") %></th>
    <th><%= N_("Cron line") %></th>
    <th><%= N_("Task count") %></th>
    <th><%= N_("Action") %></th>
    <th><%= N_("Last occurrence") %></th>
    <th><%= N_("Next occurrence") %></th>
    <th><%= N_("Current iteration") %></th>
    <th><%= N_("Iteration limit") %></th>
    <th><%= N_("Repeat until") %></th>
    <th><%= N_("State") %></th>
    <th/>
  </thead>
  <% @recurring_logics.each do |recurring_logic| %>
    <tr>
      <td><%= link_to(recurring_logic.id, foreman_tasks_recurring_logic_path(recurring_logic)) %></td>
      <td><%= recurring_logic.cron_line %></td>
      <td><%= link_to(recurring_logic.tasks.count, foreman_tasks_tasks_url(:search => "task_group.id = #{recurring_logic.task_group.id}")) %></td>
      <td><%= format_task_input(recurring_logic.tasks.first) %></td>
      <td><%= recurring_logic.tasks.order(:started_at).where('started_at IS NOT NULL').last.try(:started_at) || "-" %></td>
      <td><%= recurring_logic_next_occurrence recurring_logic %></td>
      <td><%= recurring_logic.iteration %></td>
      <td><%= format_recurring_logic_limit recurring_logic.max_iteration %></td>
      <td><%= format_recurring_logic_limit recurring_logic.end_time.try(:in_time_zone) %></td>
      <td><%= recurring_logic_state(recurring_logic) %></td>
      <td><%= recurring_logic_action_buttons(recurring_logic) %></td>
    </tr>
  <% end %>
</table>

<%= will_paginate_with_info @recurring_logics %>
